<?php
namespace Home\Controller;

class MemberController extends ParController{
    public function regist() {
        if(IS_POST){
            $model = D('Admin/Member');
            if($model->create(I('post.'),1)){
                if($model->add()){
                    $this->success('注册成功,请登录邮箱进行验证!','login');
                    exit;
                }
                
            }
            $this->error($model->getError());
        }
        //页面信息
        $this->setPageInfo('会员注册', '会员注册', '会员注册',1,array('login'));
        $this->display();
    }
    // 生成验证码的图片
    public function chkcode()
    {
       $Verify = new \Think\Verify(array(
           'length' => 4,
           'useNoise' => FALSE,
       ));
       $Verify->entry();
    }

    //登录
    public function login(){
        if(IS_POST){
            $model = D('Admin/Member');
            if($model->validate($model->_login_validate)->create(I('post.'),9)){
                if($model->login()){
                    // 从session中取出有没有要跳回的地址
                    $returnUrl = session('returnUrl');
                    if($returnUrl)
                    {
                        // 从sessoin中删除掉，下次再登录就正常跳到首页
                        session('returnUrl', null);
                        redirect($returnUrl);
                    }
                    else
                        $this->redirect('Home/Index/index');  // 登录成功之后直接跳到首页
                    
            }$this->error($model->getError());
            
            }
        }
            //页面信息
        $this->setPageInfo('会员登录', '会员登录', '会员登录',1,array('login'));
        $this->display();
        }
    

    public function emailchk(){
       $code=I('get.code');
       if($code){
           $model = M('Member');
           $email = $model->where(array('email_code'=>array('eq',$code)))->find();
           if($email){
               //设置这个账号邮箱已验证
               $model->where(array('id'=>array('eq',$email['id'])))->setField('email_code','');
               $this->success('邮箱已经完成验证','Member/login');
           }
       }
       $this->success('邮箱没有完成验证','Member/login');
    }
    
    public function logout(){
        session(null);
        $this->redirect('Home/Index/index');
    }
    //判断登录
    public function ajaxChkLogin(){
        if(session('mid')){
            $arr = array(
              'ok' =>1,
                'email'=>  session('email')
            );
        }else
            $arr=array('ok'=>0);
        
        echo json_encode($arr);
    }
    
    //判断保存登录前地址
    public function saveAndLogin(){
        session('returnUrl',$_SERVER['HTTP_REFERER']);
    }
    



}

